package com.dlust.notestxx.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.dlust.notestxx.Interface.NotesClickListener;
import com.dlust.notestxx.Model.Note;
import com.dlust.notestxx.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class NoteListAdapter extends RecyclerView.Adapter<NotesViewHolder> {
    Context context;
    List<Note> noteList;
    NotesClickListener listener;

    public NoteListAdapter(Context context, List<Note> noteList, NotesClickListener listener) {
        this.context = context;
        this.noteList = noteList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public NotesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new NotesViewHolder(LayoutInflater.from(context).inflate(R.layout.note_list, parent, false ));
    }

    @Override
    public void onBindViewHolder(@NonNull NotesViewHolder holder, int position) {
        holder.titleTxt.setText(noteList.get(position).getTitle());
        holder.notesTxt.setText(noteList.get(position).getNotes());
        holder.dateTxt.setText(noteList.get(position).getDate());
        holder.dateTxt.setSelected(true);
        if(noteList.get(position).getPinned()) {
            holder.imageView.setImageResource(R.drawable.baseline_push_pin_24);
        } else {
            holder.imageView.setImageResource(0);
        }
        int color_code = getRandomColor();
        holder.cardView.setCardBackgroundColor(holder.itemView.getResources().getColor(color_code));
        holder.cardView.setOnClickListener(v -> {
            listener.onClick(noteList.get(holder.getAdapterPosition()));
        });
        holder.cardView.setOnLongClickListener(v -> {
            listener.onLongClick(noteList.get(holder.getAdapterPosition()), holder.cardView);
            return true;
        });
    }

    private int getRandomColor() {
        List<Integer> colorCode = new ArrayList<>();
        colorCode.add(R.color.color1);
        colorCode.add(R.color.color2);
        colorCode.add(R.color.color3);
        colorCode.add(R.color.color4);
        colorCode.add(R.color.color5);
        colorCode.add(R.color.color6);
        colorCode.add(R.color.color7);
        colorCode.add(R.color.color8);
        colorCode.add(R.color.color9);
        Random random = new Random();
        int random_color = random.nextInt(colorCode.size());
        return colorCode.get(random_color);
    }

    @Override
    public int getItemCount() {
        return noteList.size();
    }
}
class NotesViewHolder extends RecyclerView.ViewHolder {
    CardView cardView;
    TextView notesTxt, titleTxt, dateTxt;
    ImageView imageView;

    public NotesViewHolder(@NonNull View itemView) {
        super(itemView);
        cardView = itemView.findViewById(R.id.note_container);
        notesTxt = itemView.findViewById(R.id.noteTxt);
        titleTxt = itemView.findViewById(R.id.titleTxt);
        dateTxt = itemView.findViewById(R.id.dateTxt);
        imageView = itemView.findViewById(R.id.pinned);
    }
}